package com.data.sql;

import com.data.entity.Tenant;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.SQLExec;
import org.springframework.util.ResourceUtils;

import java.io.File;

/**
 * 导入sql文件到数据库
 * @author ytj
 * @date 2024/4/22
 */
public class SqlExecute {
    private static final String basic="classpath:tenant_basic.sql";

    /**
     * 执行基础脚本生成数据库信息
     * @param tenant
     * */
    public static void execute(Tenant tenant) throws Exception{
        SQLExec sqlExec=new SQLExec();
        sqlExec.setDriver(tenant.getDataSourceDriver());
        sqlExec.setUrl(tenant.getDataSourceUrl());
        sqlExec.setUserid(tenant.getDataSourceUserName());
        sqlExec.setPassword(tenant.getDataSourcePassword());
        sqlExec.setEncoding("UTF-8");
        File file = ResourceUtils.getFile(basic);
        sqlExec.setSrc(file);
        sqlExec.setProject(new Project());
        sqlExec.setOutputEncoding("UTF8");
        //是否打印，默认打印
        sqlExec.setPrint(false);
        //是否自动提交
        sqlExec.setAutocommit(true);
        sqlExec.execute();
    }

}
